
package net.colah.k1.application.impl.core;

import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.Page;
import org.dayatang.querychannel.QueryChannelService;
import net.colah.k1.application.dto.*;
import net.colah.k1.application.core.InventoryApplication;
import net.colah.k1.core.*;

@Named
@Transactional
public class InventoryApplicationImpl implements InventoryApplication {


	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public InventoryDTO getInventory(Long id) {
		Inventory inventory = Inventory.load(Inventory.class, id);
		InventoryDTO inventoryDTO = new InventoryDTO();
		// 将domain转成VO
		try {
			BeanUtils.copyProperties(inventoryDTO, inventory);
		} catch (Exception e) {
			e.printStackTrace();
		}
		inventoryDTO.setId((java.lang.Long)inventory.getId());
		return inventoryDTO;
	}
	
	public InventoryDTO saveInventory(InventoryDTO inventoryDTO) {
		Inventory inventory = new Inventory();
		try {
        	BeanUtils.copyProperties(inventory, inventoryDTO);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		inventory.save();
		inventoryDTO.setId((java.lang.Long)inventory.getId());
		return inventoryDTO;
	}
	
	public void updateInventory(InventoryDTO inventoryDTO) {
		Inventory inventory = Inventory.get(Inventory.class, inventoryDTO.getId());
		// 设置要更新的值
		try {
			BeanUtils.copyProperties(inventory, inventoryDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeInventory(Long id) {
		this.removeInventorys(new Long[] { id });
	}
	
	public void removeInventorys(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Inventory inventory = Inventory.load(Inventory.class, ids[i]);
			inventory.remove();
		}
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<InventoryDTO> findAllInventory() {
		List<InventoryDTO> list = new ArrayList<InventoryDTO>();
		List<Inventory> all = Inventory.findAll(Inventory.class);
		for (Inventory inventory : all) {
			InventoryDTO inventoryDTO = new InventoryDTO();
			// 将domain转成VO
			try {
				BeanUtils.copyProperties(inventoryDTO, inventory);
			} catch (Exception e) {
				e.printStackTrace();
			}
			list.add(inventoryDTO);
		}
		return list;
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<InventoryDTO> pageQueryInventory(InventoryDTO queryVo, int currentPage, int pageSize) {
		List<InventoryDTO> result = new ArrayList<InventoryDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _inventory from Inventory _inventory   left join _inventory.product  where 1=1 ");
	
	
	
        Page<Inventory> pages = getQueryChannelService().createJpqlQuery(jpql.toString()).setParameters(conditionVals).setPage(currentPage, pageSize).pagedList();
        for (Inventory inventory : pages.getData()) {
            InventoryDTO inventoryDTO = new InventoryDTO();
            
             // 将domain转成VO
            try {
            	BeanUtils.copyProperties(inventoryDTO, inventory);
            } catch (Exception e) {
            	e.printStackTrace();
            } 
            
                             		  if(inventory.getProduct() != null) {	
			   inventoryDTO.setProductName(inventory.getProduct().getName());
		  }
                            result.add(inventoryDTO);
        }
        return new Page<InventoryDTO>(pages.getPageIndex(), pages.getResultCount(), pageSize, result);
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public ProductDTO findProductByInventory(Long id) {
		String jpql = "select e from Inventory o right join o.product e where o.id=?";
		Product result = (Product) getQueryChannelService().createJpqlQuery(jpql).setParameters(new Object[] { id }).singleResult();
		ProductDTO  dto = new ProductDTO();
		if (result != null) {
			try {
				BeanUtils.copyProperties(dto, result);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dto;
	}

	
}
